package com.lzk.jetpacktest.event.scroll

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.Scroller
import android.widget.TextView
import androidx.appcompat.widget.AppCompatTextView

/**
 * @Author: LiaoZhongKai
 * @Date: 2021/8/23 11:13
 * @Description:
 */
class ScrollEventView(context: Context,attributeSet: AttributeSet) : AppCompatTextView(context,attributeSet) {

    private var mScroller: Scroller = Scroller(context)
    private var mLastX = 0F
    private var mLastY = 0F

    override fun onTouchEvent(event: MotionEvent): Boolean {
        val x = event.rawX
        val y = event.rawY
        when(event.action){
            MotionEvent.ACTION_DOWN -> {
            }
            MotionEvent.ACTION_MOVE -> {
                val deltaX = x - mLastX
                val deltaY = y - mLastY
                val transX = translationX+deltaX
                val transY = translationY+deltaY

                val viewGroup = parent as ViewGroup
                val parentW = viewGroup.width
                val parentH = viewGroup.height

                if (transX > 0){
                    if (left+width+transX <= parentW){
                        translationX = transX
                    }
                }else {
                    if (left +transX >= 0){
                        translationX = transX
                    }
                }
              if (transY > 0){
                  if (top+height+transY <= parentH){
                      translationY = transY
                  }
              }else{
                  if (top + transY >= 0 ){
                      translationY = transY
                  }
              }
            }
            MotionEvent.ACTION_UP -> {
            }
        }
        mLastX = x
        mLastY = y
        return true
    }


}